﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KimPhongCRM.Models;
using DevExpress.Web.Mvc;

namespace KimPhongCRM.Controllers
{
    public class RolePermissionController : Controller
    {
        private Entities db = new Entities();

        //
        // GET: /RolePermission/

        public ViewResult Index()
        {
            foreach (var role in db.Roles.ToList())
            {
                foreach (var perm in db.tblPermissions.ToList())
                {
                    if (!db.tblRolePermissions.Any(o => o.RoleId == role.RoleId && o.permId == perm.permId))
                    {
                        tblRolePermission rolePerm = new tblRolePermission();
                        rolePerm.permId = perm.permId;
                        rolePerm.RoleId = role.RoleId;
                        rolePerm.isActive = false;
                        db.tblRolePermissions.Add(rolePerm);
                        db.SaveChanges();
                    }
                }
            }
            return View(db.tblRolePermissions.ToList());
        }

        //
        // GET: /RolePermission/Details/5

        public ViewResult Details(Guid id)
        {
            tblRolePermission tblrolepermission = db.tblRolePermissions.Find(id);
            return View(tblrolepermission);
        }

        //
        // GET: /RolePermission/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /RolePermission/Create

        [HttpPost]
        public ActionResult Create(tblRolePermission tblrolepermission)
        {
            if (ModelState.IsValid)
            {
                tblrolepermission.permId = Guid.NewGuid();
                db.tblRolePermissions.Add(tblrolepermission);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(tblrolepermission);
        }

        //
        // GET: /RolePermission/Edit/5

        public ActionResult Edit(Guid id)
        {
            tblRolePermission tblrolepermission = db.tblRolePermissions.Find(id);
            return View(tblrolepermission);
        }

        //
        // POST: /RolePermission/Edit/5

        [HttpPost]
        public ActionResult Edit(tblRolePermission tblrolepermission)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tblrolepermission).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(tblrolepermission);
        }

        //
        // GET: /RolePermission/Delete/5

        public ActionResult Delete(Guid id)
        {
            tblRolePermission tblrolepermission = db.tblRolePermissions.Find(id);
            return View(tblrolepermission);
        }

        //
        // POST: /RolePermission/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(Guid id)
        {
            tblRolePermission tblrolepermission = db.tblRolePermissions.Find(id);
            db.tblRolePermissions.Remove(tblrolepermission);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        KimPhongCRM.Models.Entities db1 = new KimPhongCRM.Models.Entities();

        [ValidateInput(false)]
        public ActionResult listRolePermissionPartial()
        {
            setViewData();
            var model = db1.tblRolePermissions;
            return PartialView("_listRolePermissionPartial", model.ToList());
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult listRolePermissionPartialAddNew(KimPhongCRM.Models.tblRolePermission item)
        {
            setViewData();
            var model = db1.tblRolePermissions;
            if (ModelState.IsValid)
            {
                try
                {
                    model.Add(item);
                    db1.SaveChanges();
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }
            else
                ViewData["EditError"] = "Please, correct all errors.";
            return PartialView("_listRolePermissionPartial", model.ToList());
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult listRolePermissionPartialUpdate(KimPhongCRM.Models.tblRolePermission item)
        {
            setViewData();
            var model = db1.tblRolePermissions;
            if (ModelState.IsValid)
            {
                try
                {
                    var modelItem = model.FirstOrDefault(it => it.permId == item.permId);
                    if (modelItem != null)
                    {
                        this.UpdateModel(modelItem);
                        db1.SaveChanges();
                    }
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }
            else
                ViewData["EditError"] = "Please, correct all errors.";
            return PartialView("_listRolePermissionPartial", model.ToList());
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult listRolePermissionPartialDelete(System.Guid permId)
        {
            setViewData();
            var model = db1.tblRolePermissions;
            if (permId != null)
            {
                try
                {
                    var item = model.FirstOrDefault(it => it.permId == permId);
                    if (item != null)
                        model.Remove(item);
                    db1.SaveChanges();
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }
            return PartialView("_listRolePermissionPartial", model.ToList());
        }
        //function
        public void setViewData()
        {
            ViewData["Roles"] = db.sp_Select_Combobox_Roles().ToList();
            ViewData["Permission"] = db.sp_Select_Combobox_Permission().ToList();
        }
    }

}